// redux -> dva
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
// import { Reducer } from 'umi';

export interface GlobalState {
  token: string;
}

export interface GlobalModelProps {
  namespace: 'global';
  state: GlobalState;
  effects: {
    getLogin: Effect;
  };
  reducers: {
    save: Reducer<GlobalState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  // subscriptions?: { setup: Subscription };
}


const global:GlobalModelProps = {
  namespace: "global",
  state: {
    token: '我是测试token'
  },
  // 副作用。想直接接收请求数据结果
  effects: {
    // payload 参数  call 请求  put 发送--dispatch
    *getLogin({ payload }, { call, put }) {
      const data = yield call(createData, { page: 1, size: 5 })
      console.log('data:', data)
      yield put({
        type: 'save',
        data
      })
      // 加这个return，就是让 dispatch可以直接在组件内部获取结果
      return { data }
    }
  },
  // 接收处理逻辑，返回数据
  reducers: {
    save(state:any, action:any) {
      console.log('state:', state, action)
      return {...state}
    }
  }
}
export default global;




interface CreateDataProps {
  page: number,
  size: number
}

function createData ({page, size}: CreateDataProps): any {
  return Promise.resolve({
    code: 0,
    msg: 'success',
    payload: { page, size }
  })
}
